package net.jlibrary.computer.action;

import net.jlibrary.common.CoreConstants;
import net.jlibrary.common.core.entity.Pagination;
import net.jlibrary.common.core.entity.PaginationParam;
import net.jlibrary.computer.action.vo.ComputerTypeVo;

import net.jlibrary.computer.service.IComputerTypeManager;

import org.apache.commons.lang.StringUtils;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/computer")
public class ComputerTypeAction {
    @Resource
    private IComputerTypeManager computerTypeManager;

    @RequestMapping(value = "/computerTypeView", method = {RequestMethod.GET})
    public ModelAndView view() {
        return new ModelAndView("/computer/computerTypeView");
    }


    @RequestMapping(value = "/selectComputerTypeList", method = {RequestMethod.POST})
    public Pagination computerData(ComputerTypeVo computerVo, PaginationParam param) {
        Pagination pagination = computerTypeManager.listsPage(computerVo, param);
        return pagination;
    }

    //	表单初始化信息
    @RequestMapping(value = "/computerTypeFormInit", method = {RequestMethod.GET})
    public ComputerTypeVo computerFormInit() {
        ComputerTypeVo computerVo = new ComputerTypeVo();
        return computerVo;
    }

    //	保存电脑信息
    @RequestMapping(value = "/saveComputerType", method = {RequestMethod.POST})
    public ModelMap save(@ModelAttribute("vm") ComputerTypeVo computerVo) {
        computerTypeManager.save(computerVo);
        ModelMap modelMap = new ModelMap();
        modelMap.put("success", true);
        return modelMap;
    }

    //	详细单个电脑信息
    @RequestMapping(value = "/detailComputerType", method = {RequestMethod.GET})
    public ComputerTypeVo detail(Long id) {
        ComputerTypeVo computerTypeVo = new ComputerTypeVo();
        computerTypeVo.setId(id);
        ComputerTypeVo computerVo = computerTypeManager.get(computerTypeVo);
        return computerVo;
    }

    //	删除电脑信息
    @RequestMapping(value = "/deleteComputerType", method = {RequestMethod.POST})
    public ModelMap delete(Long id) {

        ComputerTypeVo computerVo = new ComputerTypeVo();
        computerVo.setId(id);
        computerTypeManager.delete(computerVo);
        ModelMap modelMap = new ModelMap();
        modelMap.put("success", true);
        return modelMap;
    }

    //	修改数据
    @RequestMapping(value = "/updateComputerType", method = {RequestMethod.POST})
    public ModelMap update(@ModelAttribute("vm") ComputerTypeVo computerVo) {
        computerTypeManager.update(computerVo);
        ModelMap modelMap = new ModelMap();
        modelMap.put("success", true);
        return modelMap;
    }

    //	恢复电脑信息
    @RequestMapping(value = "/renewComputerType", method = {RequestMethod.POST})
    public ModelMap renew(Long id) {
        ComputerTypeVo computerVo = new ComputerTypeVo();
        computerVo.setId(id);
        computerTypeManager.renew(computerVo);
        ModelMap modelMap = new ModelMap();
        modelMap.put("success", true);
        return modelMap;
    }

    @RequestMapping(value = "/specialtyJsonData", method = {RequestMethod.GET})
    public List<ModelMap> specialtyJsonData() {
        List<ComputerTypeVo> specialtyVos = computerTypeManager.lists(new ComputerTypeVo());
        List<ModelMap> maps = new ArrayList<ModelMap>();
        for (ComputerTypeVo specialtyVo : specialtyVos) {
            ModelMap map = new ModelMap();
            map.put(CoreConstants.VIEW_SELECT_LABEL_VALUE, specialtyVo.getPrimaryKey());
            map.put(CoreConstants.VIEW_SELECT_LABEL_TEXT, specialtyVo.getText());
            maps.add(map);
        }
        return maps;
    }

}
